package com.liu.jpa.controller;

import com.liu.jpa.dao.UserRepository;
import com.liu.jpa.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserRepository repository;

    @RequestMapping("/add")
    public String add(){
        User u = null;
        for (int i = 0; i < 10; i++){
            u = new User();
            u.setUsername("zhangsan" + i);
            u.setGender(i % 2 ==0 ? "男" : "女");
            u.setAge(i);
            u.setId(i);
            repository.save(u);
        }
        return "添加成功";
    }

    @RequestMapping("/find")
    public List<User> find(){
        return repository.findAll();
    }
}
